/**
 * <p>Title: StringArrayConverter.java</p>
 * <p>Description: DoNotes</p>
 * @author WangJun
 * @date 2009/11/17 23:22:59
 * @version 1.0
 */
package net.donotes.util.converter;

import java.util.Map;

import ognl.DefaultTypeConverter;

/**
 * @author WangJun
 * 
 */
public class StringArrayConverter extends DefaultTypeConverter {
	@SuppressWarnings("unchecked")
	@Override
	public Object convertValue(Map map, Object obj, Class toType) {
		if (toType == String[].class) {
			String str = ((String[]) obj)[0];
			return str.split(" ");
		} else if (toType == String.class) {
			String[] list = (String[]) obj;
			return join(list);
		}
		return null;
	}

	private String join(String[] list) {
		StringBuilder sb = new StringBuilder("");
		for (String s : list) {
			sb.append(s);
			sb.append(" ");
		}
		return sb.toString();
	}
}
